#define _CRT_SECURE_NO_WARNINGS

#include "base.h"
#include <iostream>
#include "child.h"
#include "string.h"

Base::Base(int _PC)
{
	index = _PC;
}

Base* Base::initInstruction(char* buffer)
{
	int index, dReg, sReg, reg1, reg2, intVal;
	char name[10];
	sscanf(buffer, "%i:%s", &index, name);
	if (strcmp(name, "LW") == 0)
	{
		sscanf(buffer, "%d: LW r%i,%i[r%i]", &index, &dReg, &intVal, &sReg);
		return new Lw(index, dReg, sReg, intVal);
	}
	if (strcmp(name, "SW") == 0)
	{
		sscanf(buffer, "%d: SW r%i, %i[r%i]", &index, &sReg, &intVal, &dReg);
		return new Sw(index, dReg, sReg, intVal);
	}
	if (strcmp(name, "LWI") == 0)
	{
		sscanf(buffer, "%d: LWI r%i, %i", &index, &dReg, &intVal);
		return new Lwi(index, dReg, intVal);
	}
	if (strcmp(name, "IADD") == 0)
	{
		sscanf(buffer, "%d: IADD r%i, r%i, r%i", &index, &dReg, &reg1, &reg2);
		return new Iadd(index, dReg, reg1, reg2);
	}
	if (strcmp(name, "ISUB") == 0)
	{
		sscanf(buffer, "%d: ISUB r%i, r%i, r%i", &index, &dReg, &reg1, &reg2);
		return new Isub(index, dReg, reg1, reg2);
	}
	if (strcmp(name, "IMUL") == 0)
	{
		sscanf(buffer, "%d: IMUL r%i, r%i, r%i", &index, &dReg, &reg1, &reg2);
		return new Imul(index, dReg, reg1, reg2);
	}
	if (strcmp(name, "IDIV") == 0)
	{
		sscanf(buffer, "%d: IDIV r%i, r%i, r%i", &index, &dReg, &reg1, &reg2);
		return new Idiv(index, dReg, reg1, reg2);
	}
	if (strcmp(name, "IMOD") == 0)
	{
		sscanf(buffer, "%d: IMOD r%i, r%i, r%i", &index, &dReg, &reg1, &reg2);
		return new Imod(index, dReg, reg1, reg2);
	}
	return NULL;
}
